package com.zf.actuator.endpoint.metric;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ThreadLocalRandom;

/**
 * 修改指标对象的值
 */
@RestController
@RequestMapping("/metric")
public class MetricController {

    @Autowired
    private MeterRegistry meterRegistry;

    /**
     * 修改 自定义的指标对应的值，见 {@link MyCustomMetricConfig} 类
     */
    @GetMapping("/incr")
    public String incr() {
        // 修改对象的值
        MyCustomMetricConfig.DataObj.ADDER.addAndGet(3);
        MyCustomMetricConfig.DataObj.AGE.set(ThreadLocalRandom.current().nextInt(5, 10));

        // 从 MeterRegistry 中获取指定的 计数器 Meter
        Counter counter = meterRegistry.get(MyCustomMetricConfig.MY_NUMBER_COUNTER_1).counter();
        // 自增 1
        counter.increment();

        // 修改对象的值
        MyCustomMetricConfig.DataObj.MONEY.addAndGet(100);

        return "ok";
    }
}
